<template>
  <el-dialog title="分配角色" :visible="visible" :before-close="closeFn" @open="getRoleList">
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting.js'
import { getUserDetailById, assignRoles } from '@/api/user'
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    employeeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      checkList: [],
      list: []
    }
  },
  methods: {
    closeFn() {
      this.$emit('update:visible', false)
    },
    async getRoleList() {
      this.getRole()
      this.getUserDetail()
    },
    async getRole() {
      const { rows } = await getRoleList({
        page: 1,
        pagesize: 9999999999999999
      })
      this.list = rows
    },
    async getUserDetail() {
      const { roleIds } = await getUserDetailById(this.employeeId)
      this.checkList = roleIds || []
    },
    async onSubmit() {
      if (!this.checkList.length) return this.$message.warning('分配角色不能为空')
      await assignRoles({
        id: this.employeeId,
        roleIds: this.checkList
      })
      this.$message.success('分配角色成功')
      this.checkList = []
      this.$emit('update:visible', false)
    }
  }
}
</script>
